Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
control.c File Reference
+ Include dependency graph for control.c:

Go to the source code of this file.

Macros

#define F_USE_WIRELESS_UART   0x0001
 
#define F_USE_WIRED_UART   0x0002
 
#define CONTROL_BAUDRATE   115200
 

Functions

void myUART_WriteByte (UART_Type *base, uint8_t data)
 
int8_t writeControlPort (ControlSubsystem *pComm, uint8_t buffer[], uint16_t nbytes)
 
int8_t writeWirelessPort (uint8_t buffer[], uint16_t nbytes)
 
void WIRELESS_UART_IRQHandler (void)
 
void BlueRadios_Init (void)
 
void echo (uint8_t data)
 
void WIRED_UART_IRQHandler (void)
 
int8_t initializeControlPort (ControlSubsystem *pComm)
 

Variables

uart_handle_t wired_uartHandle
 
uart_handle_t wireless_uartHandle
 
uint8_t sUARTOutputBuffer [256]
 
SensorFusionGlobals sfg
 

Detailed Description

This file contains a UART implementation of the control subsystem. The command interpreter and streaming functions are contained in two separate files. So you can easily swap those out with only minor changes here.

Definition in file control.c.

Macro Definition Documentation

#define CONTROL_BAUDRATE   115200

Baudrate to be used for serial communications.

Definition at line 53 of file control.c.

Referenced by initializeControlPort().

#define F_USE_WIRED_UART   0x0002

0x0002 to include, 0x0000 otherwise

Definition at line 50 of file control.c.

#define F_USE_WIRELESS_UART   0x0001

0x0001 to include, 0x0000 otherwise

Definition at line 47 of file control.c.

Function Documentation

void BlueRadios_Init ( void  )

Used to initialize the Blue Radios Bluetooth module found on the FRDM-FXS-MULT2-B sensor shield from NXP.

Definition at line 132 of file control.c.

Referenced by initializeControlPort().

133 {
134  uint16_t ilen; // command string length
135 
136  // transmit "ATSRM,2,0\r" to minimize traffic from the module
137  // command "ATSRM": sets the module response mode which configures how verbose the module will be
138  // 2: response mode at to minimal
139  // 0: disconnected mode is command mode
140  // \r: carriage return escape sequence
141  strcpy((char *)sUARTOutputBuffer, "ATSRM,2,0\r");
142  ilen = strlen((char *)sUARTOutputBuffer);
144  return;
145 }
int8_t writeWirelessPort(uint8_t buffer[], uint16_t nbytes)
Definition: control.c:99
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:59

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void echo ( uint8_t  data)

Definition at line 149 of file control.c.

150 {
151  /* Send data only when UART TX register is empty and ring buffer has data to send out. */
152  if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(WIRED_UART))
153  {
154  UART_WriteByte(WIRED_UART, data);
155  }
156 }
int8_t initializeControlPort ( ControlSubsystem pComm)

Initialize the control subsystem and all related hardware.

Call this once to initialize structures, ports, etc.

Parameters
pCommpointer to the control subystem structure

Definition at line 182 of file control.c.

Referenced by main().

185 {
186  uart_config_t config;
187  if (pComm)
188  {
189  pComm->DefaultQuaternionPacketType = Q3; // default to simplest algorithm
190  pComm->QuaternionPacketType = Q3; // default to simplest algorithm
191  pComm->AngularVelocityPacketOn = true; // transmit angular velocity packet
192  pComm->DebugPacketOn = true; // transmit debug packet
193  pComm->RPCPacketOn = true; // transmit roll, pitch, compass packet
194  pComm->AltPacketOn = true; // Altitude packet
195  pComm->AccelCalPacketOn = 0;
196  pComm->write = writeControlPort;
197  pComm->stream = CreateAndSendPackets;
198 
199 #if F_USE_WIRED_UART
200  /* Initialize WIRED UART pins below - currently duplicates code in pin_mux.c */
201  CLOCK_EnableClock(WIRED_UART_PORT_CLKEN);
202  PORT_SetPinMux(WIRED_UART_PORT, WIRED_UART_RX_PIN, WIRED_UART_MUX);
203  PORT_SetPinMux(WIRED_UART_PORT, WIRED_UART_TX_PIN, WIRED_UART_MUX);
204  UART_GetDefaultConfig(&config);
205 
206  config.baudRate_Bps = CONTROL_BAUDRATE;
207  config.enableTx = true;
208  config.enableRx = true;
209  config.rxFifoWatermark = 1;
210  UART_Init(WIRED_UART, &config, CLOCK_GetFreq(WIRED_UART_CLKSRC));
211 
212  /* Enable RX interrupt. */
213  UART_EnableInterrupts(WIRED_UART, kUART_RxDataRegFullInterruptEnable |
214  kUART_RxOverrunInterruptEnable);
215  EnableIRQ(WIRED_UART_IRQn);
216 #endif
217 #if F_USE_WIRELESS_UART
218  /* Initialize WIRELESS UART pins below */
219  CLOCK_EnableClock(WIRELESS_UART_PORT_CLKEN);
220  PORT_SetPinMux(WIRELESS_UART_PORT, WIRELESS_UART_RX_PIN,
221  WIRELESS_UART_MUX);
222  PORT_SetPinMux(WIRELESS_UART_PORT, WIRELESS_UART_TX_PIN,
223  WIRELESS_UART_MUX);
224 
225  UART_Init(WIRELESS_UART, &config, CLOCK_GetFreq(WIRELESS_UART_CLKSRC));
226  BlueRadios_Init();
227 
228  /* Enable RX interrupt. */
229  UART_EnableInterrupts(WIRELESS_UART, kUART_RxDataRegFullInterruptEnable |
230  kUART_RxOverrunInterruptEnable);
231  EnableIRQ(WIRELESS_UART_IRQn);
232 #endif
233 
234  return (0);
235  }
236  else
237  {
238  return (1);
239  }
240 }
volatile uint8_t AltPacketOn
flag to enable altitude packet
Definition: control.h:70
streamData_t * stream
function to create packets for serial stream
Definition: control.h:73
volatile int8_t AccelCalPacketOn
variable used to coordinate accelerometer calibration
Definition: control.h:71
quaternion_type DefaultQuaternionPacketType
default quaternion transmitted at power on
Definition: control.h:65
int8_t writeControlPort(ControlSubsystem *pComm, uint8_t buffer[], uint16_t nbytes)
Definition: control.c:79
void CreateAndSendPackets(SensorFusionGlobals *sfg, uint8_t *sUARTOutputBuffer)
Called once per fusion cycle to stream information required by the NXP Sensor Fusion Toolbox...
volatile uint8_t DebugPacketOn
flag to enable debug packet
Definition: control.h:68
writePort_t * write
low level function to write a char buffer to the serial stream
Definition: control.h:72
void BlueRadios_Init(void)
Used to initialize the Blue Radios Bluetooth module found on the FRDM-FXS-MULT2-B sensor shield from ...
Definition: control.c:132
volatile uint8_t RPCPacketOn
flag to enable roll, pitch, compass packet
Definition: control.h:69
Quaternion derived from 3-axis accel (tilt)
Definition: sensor_fusion.h:65
#define CONTROL_BAUDRATE
Baudrate to be used for serial communications.
Definition: control.c:53
volatile quaternion_type QuaternionPacketType
quaternion type transmitted over UART
Definition: control.h:66
volatile uint8_t AngularVelocityPacketOn
flag to enable angular velocity packet
Definition: control.h:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void myUART_WriteByte ( UART_Type *  base,
uint8_t  data 
)

Definition at line 68 of file control.c.

Referenced by writeControlPort(), and writeWirelessPort().

69 {
70  uint8_t flag = 0;
71  while (!flag)
72  {
73  flag = base->S1 & 0x80;
74  }
75  UART_WriteByte(base, data);
76 }

+ Here is the caller graph for this function:

void WIRED_UART_IRQHandler ( void  )

Definition at line 158 of file control.c.

159 {
160  uint8_t data;
161  status_t sts;
162  uint32_t nbytes; // number of bytes received
163  uint32_t flags;
164  static char iCommandBuffer_A[5] = "~~~~"; // 5 bytes long to include the unused terminating \0
165 
167  flags = UART_GetStatusFlags(WIRED_UART);
168  /* If new data arrived. */
169  if ((kUART_RxDataRegFullFlag | kUART_RxOverrunFlag) & flags)
170  {
171  sts = UART_TransferGetReceiveCount(WIRED_UART, &wired_uartHandle, &nbytes);
172  if (sts == kStatus_Success)
173  {
174  data = UART_ReadByte(WIRED_UART);
175  DecodeCommandBytes(&sfg, iCommandBuffer_A, &data, 1);
176  }
177  }
178 }
uart_handle_t wired_uartHandle
Definition: control.c:55
Receiving commands over wired interface (momentary)
void DecodeCommandBytes(SensorFusionGlobals *sfg, char iCommandBuffer[], uint8 sUART_InputBuffer[], uint16 nbytes)
This function is responsible for decoding commands sent by the NXP Sensor Fusion Toolbox and setting ...
setStatus_t * setStatus
change status indicator immediately
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.

+ Here is the call graph for this function:

void WIRELESS_UART_IRQHandler ( void  )

Definition at line 110 of file control.c.

111 {
112  uint8_t data;
113  status_t sts;
114  uint32_t nbytes; // number of bytes received
115  uint32_t flags;
116  static char iCommandBuffer_B[5] = "~~~~"; // 5 bytes long to include the unused terminating \0
118  flags = UART_GetStatusFlags(WIRELESS_UART);
119  /* If new data arrived. */
120  if ((kUART_RxDataRegFullFlag | kUART_RxOverrunFlag) & flags)
121  {
122  sts = UART_TransferGetReceiveCount(WIRELESS_UART, &wireless_uartHandle, &nbytes);
123  if (sts == kStatus_Success)
124  {
125  data = UART_ReadByte(WIRELESS_UART);
126  DecodeCommandBytes(&sfg, iCommandBuffer_B, &data, 1);
127  }
128  }
129 }
Receiving commands over wireless interface (momentary)
void DecodeCommandBytes(SensorFusionGlobals *sfg, char iCommandBuffer[], uint8 sUART_InputBuffer[], uint16 nbytes)
This function is responsible for decoding commands sent by the NXP Sensor Fusion Toolbox and setting ...
uart_handle_t wireless_uartHandle
Definition: control.c:56
setStatus_t * setStatus
change status indicator immediately
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.

+ Here is the call graph for this function:

int8_t writeControlPort ( ControlSubsystem pComm,
uint8_t  buffer[],
uint16_t  nbytes 
)

Definition at line 79 of file control.c.

Referenced by initializeControlPort().

80 {
81  uint16_t i;
82  for (i = 0; i < nbytes; i++)
83  {
84 #if F_USE_WIRED_UART
85  myUART_WriteByte(WIRED_UART, buffer[i]);
86 #endif
87 #if F_USE_WIRELESS_UART
88  myUART_WriteByte(WIRELESS_UART, buffer[i]);
89 #endif
90  }
91 
92  return (0);
93 }
void myUART_WriteByte(UART_Type *base, uint8_t data)
Definition: control.c:68

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int8_t writeWirelessPort ( uint8_t  buffer[],
uint16_t  nbytes 
)

Definition at line 99 of file control.c.

Referenced by BlueRadios_Init().

100 {
101  uint16_t i;
102  for (i = 0; i < nbytes; i++)
103  {
104  myUART_WriteByte(WIRELESS_UART, buffer[i]);
105  }
106 
107  return (0);
108 }
void myUART_WriteByte(UART_Type *base, uint8_t data)
Definition: control.c:68

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

This is the primary sensor fusion data structure.

Definition at line 62 of file main_agm01_freertos_two_tasks.c.

Referenced by FXOS8700_Init(), and initializeSensors().

uint8_t sUARTOutputBuffer[256]

main output buffer defined in control.c

Definition at line 59 of file control.c.

Referenced by BlueRadios_Init(), fusion_task(), main(), and read_task().

uart_handle_t wired_uartHandle

Definition at line 55 of file control.c.

Referenced by WIRED_UART_IRQHandler().

uart_handle_t wireless_uartHandle

Definition at line 56 of file control.c.

Referenced by WIRELESS_UART_IRQHandler().